package com.alipay.app.groupmeal.standard.ui.c.pre;


import android.os.Build;

import android.util.Log;
import android.view.Display;

import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;


import androidx.constraintlayout.widget.ConstraintLayout;

import com.alipay.app.groupmeal.standard.R;
import com.alipay.app.groupmeal.standard.base.BasePresentation;
import com.alipay.app.groupmeal.standard.bean.AliPayResVo;
import com.alipay.app.groupmeal.standard.constant.BaseConstant;
import com.alipay.app.groupmeal.standard.constant.ConfigConstant;
import com.alipay.app.groupmeal.standard.http.bean.resp.QrPayModel;
import com.alipay.app.groupmeal.standard.http.bean.resp.Result;
import com.alipay.app.groupmeal.standard.ui.c.SelectPayActivity;
import com.alipay.app.groupmeal.standard.ui.b.util.ScanKeyManager;
import com.alipay.app.groupmeal.standard.ui.b.util.WIth;

import java.util.LinkedList;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.internal.observers.BlockingBaseObserver;


public class ScanQrMainPre extends BasePresentation {

    private ScanKeyManager scanKeyManager;

    private ProgressBar progressLoading_b;

    private SelectPayActivity mActivity;

    private ConstraintLayout loadingCon;

    private ImageView imageView;

    private LinearLayout linear;

    private TextView tvLoading_b;

    private TextView tv_close_time;

    private String amount;

    private NetRes netRes;

    public void setNetRes(NetRes netRes) {
        this.netRes = netRes;
    }

    public ScanQrMainPre(SelectPayActivity outerContext, Display display, String amount) {
        super(outerContext, display);
        mActivity = outerContext;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        } else {
            getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        }
        this.amount = amount;
    }

    @Override
    protected int getLayoutId() {
        if (ConfigConstant.displayType == BaseConstant.DISPLAY_TYPE.T2) {
            return R.layout.activity_scan_qr_ft2;
        } else {
            return R.layout.activity_scan_qr;
        }
    }

    @Override
    protected void initView() {
        loadingCon = findViewById(R.id.loadingCon);
        progressLoading_b = findViewById(R.id.progressLoading_b);
        imageView = findViewById(R.id.iconImg);
        linear = findViewById(R.id.linear);
        tvLoading_b = findViewById(R.id.tvLoading_b);
        tv_close_time = findViewById(R.id.tv_close_time);
        scanKeyManager = new ScanKeyManager((String value) -> {
            Log.i("MainActivity", "ScanValue->" + value);
            mActivity.runOnUiThread(() -> {
                startQR(value);
            });
        });
    }

    String order;

    @Override
    public boolean onKeyEventBack(KeyEvent event) {
        mActivity.onKeyEventBack(event);
        return super.onKeyEventBack(event);
    }

    private String auth = "";

    private void startQR(String str) {
        if (auth == null || "".equals(auth)) {
            auth = str;
        } else {
            return;
        }
        order = String.valueOf(System.currentTimeMillis());
        linear.setVisibility(View.GONE);
        imageView.setVisibility(View.GONE);
        loadingCon.setVisibility(View.VISIBLE);
        if (str.length() >= 18 && "1".equals(str.substring(0, 1))) {
            progressLoading_b.setIndeterminateDrawable(this.getResources().getDrawable(R.drawable.progress_loading_wechat));
        }

        new WIth(mActivity).qrPayV2(amount, str, order,"", new LinkedList<>(), new BlockingBaseObserver<Result<AliPayResVo>>() {
            @Override
            public void onNext(@NonNull Result<AliPayResVo> aliPayResVoResult) {
                if (netRes != null) {
                    netRes.netRes(aliPayResVoResult, null);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (netRes != null) {
                    netRes.netRes(null, e);
                }
            }
        });

    }

    MyThread1 myThread1 = new MyThread1();

    private class MyThread1 extends Thread {

        //在run方法里复写需要进行的操作:卖票速度1s/张
        @Override
        public void run() {
            try {
                sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            new WIth(mActivity).checkQrOrders(order, new BlockingBaseObserver<Result<QrPayModel>>() {
                @Override
                public void onNext(@NonNull Result<QrPayModel> qrPayModelResult) {
                    if (qrPayModelResult.getResult().getCode() == 1) {
                        mActivity.runOnUiThread(() -> {
                            tvLoading_b.setText("支付成功");
                        });
                    } else if (qrPayModelResult.getResult().getCode() == 0) {
                        mActivity.runOnUiThread(() -> {
                            tvLoading_b.setText(qrPayModelResult.getResult().getMsg());
                        });
                        new Thread(myThread1).start();
                    } else {
                        mActivity.runOnUiThread(() -> {
                            tvLoading_b.setText(qrPayModelResult.getResult().getMsg());
                        });
                    }
                }

                @Override
                public void onError(@NonNull Throwable e) {

                }
            });
        }
    }


    @Override
    protected void initData() {

    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_ESCAPE) {
            mActivity.dispatchKeyEvent(event);
        } else if (event.getKeyCode() != KeyEvent.KEYCODE_BACK) {
            scanKeyManager.analysisKeyEvent(event);
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    public TextView getTvCloseTime() {
        return tv_close_time;
    }

    public void setTvCloseTime(TextView tv_close_time) {
        this.tv_close_time = tv_close_time;
    }

    public interface NetRes {
        void netRes(Result<AliPayResVo> aliPayResVoResult, Throwable e);
    }
}